//
//  XMGNavigationController.m
//  百思不得解国内
//
//  Created by sunnyday on 16/5/17.
//  Copyright © 2016年 sunnyday. All rights reserved.
//

#import "XMGNavigationController.h"

@interface XMGNavigationController ()

@end


@implementation XMGNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];
}


/**
 * 当第一次使用这个类的时候会调用一次
 */

+ (void)initialize
{
    // 当导航栏用在XMGNavigationController中, appearance设置才会生效
    //    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];

    UINavigationBar *bar = [UINavigationBar appearance];
    [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.childViewControllers.count > 0 ) {
        // 如果push进来的不是第一个控制器
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"返回" forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
        button.size = CGSizeMake(70, 30);
        //让按钮 内部所有的按钮  都左对齐
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //设置 内边距 让button 里的东西出来   附属就可以了
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
        // 因为啊扭是自定义的  所以  要给按钮 添加 点击事件
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        // 设置在左边
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        // 在这里面 要隐藏 tabbar  不能放倒外面
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    // 这个  super 要放倒外面 让view viewcontroller 可以覆盖 上 面的设置  leftBarButtonItem
    
    [super pushViewController:viewController animated:animated];
    
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}
@end
